package com.zhixianggou.caibaobox.commons.result;

import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@AllArgsConstructor
@Accessors(chain = true)
public class ReturnData<T> implements Serializable {

    private String code;
    private String messaqe;

    private T data;

    private long timestamp;

    public ReturnData() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ReturnData<T> success(){
        ReturnData returnData = new ReturnData();
        returnData.setCode(ReturnCodeEnum.RC200.getCode())
                  .setMessaqe(ReturnCodeEnum.RC200.getMessage())
                  .setData(null);
        return returnData;
    }

    public static <T> ReturnData<T> success(T data){
        ReturnData returnData = new ReturnData();
        returnData.setCode(ReturnCodeEnum.RC200.getCode())
                  .setMessaqe(ReturnCodeEnum.RC200.getMessage())
                  .setData(data);
        return returnData;
    }

    public static <T> ReturnData<T> fail(){
        ReturnData returnData = new ReturnData();
        returnData.setCode(ReturnCodeEnum.RC500.getCode())
                  .setMessaqe(ReturnCodeEnum.RC500.getMessage());
        return returnData;
    }
    public static <T> ReturnData<T> fail(String code,String messaqe){
        ReturnData returnData = new ReturnData();
        returnData.setCode(code)
                  .setMessaqe(messaqe);
        return returnData;
    }
    public static <T> ReturnData<T> fail(ReturnCodeEnum codeEnum){
        ReturnData returnData = new ReturnData();
        returnData.setCode(codeEnum.getCode())
                  .setMessaqe(codeEnum.getMessage());
        return returnData;
    }



    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
